# 计算协方差
import numpy as np


def mean_var(array_1d):
    # 计算均值
    mean_value = np.mean(array_1d)
    print("均值:", mean_value)
    # 计算方差
    variance = np.var(array_1d)
    print("方差:", variance)
    # 计算样本方差
    sample_variance = np.var(array_1d, ddof=1)
    print("样本方差:", sample_variance)


# 您的数据
data = [
    [-1.5, 2.3, -3.4, -9.5, 1.3, 2.2, -3.3, 10.0],
    [-1.3, 4.3, -5.4, 10.5, 3.5, 2.5, 0.3, -9.7]
]

mean_var(data[0])
mean_var(data[1])

# 将数据转换为NumPy数组
data_np = np.array(data)
# 计算协方差矩阵
covariance_matrix = np.cov(data_np, rowvar=True)
print(covariance_matrix)
print(covariance_matrix.shape)

# 假设你有两个一维数组
x = np.array(data[0])
y = np.array(data[1])

# 计算均值
mean_x = np.mean(x)
mean_y = np.mean(y)

# 计算协方差
covariance = np.sum((x - mean_x) * (x - mean_y)) / (len(x) - 1)
print("协方差:", covariance)
covariance = np.sum((x - mean_x) * (y - mean_y)) / (len(x) - 1)
print("协方差:", covariance)
covariance = np.sum((y - mean_x) * (x - mean_y)) / (len(x) - 1)
print("协方差:", covariance)
covariance = np.sum((y - mean_x) * (y - mean_y)) / (len(x) - 1)
print("协方差:", covariance)
